package com.nmgolden.lanxin.api;

import com.alibaba.fastjson.JSONObject;
import com.nmgolden.lanxin.LanxinHttpClient;
import com.nmgolden.lanxin.LanxinAppConfig;
import com.nmgolden.lanxin.LanxinTokenManager;

/**
 * @author siger
 * @created 2024/5/18
 * @updated 2024/5/18
 */
public class AuthApi extends AbsApi {
    public AuthApi(LanxinAppConfig config, LanxinHttpClient lanxinHttpClient, LanxinTokenManager lanxinTokenManager) {
        super(config, lanxinHttpClient, lanxinTokenManager);
    }

    /**
     * 身份识别码获取人员信息
     * 根据人员身份识别码获取组织人员ID，详细说明参考身份识别码获取人员身份。
     * 详情 <a href="https://developer.lanxin.cn/official/article?id=646eca933d4e4adb7039c0e1&module=back-end-api&article_id=646ed2433d4e4adb7039c114">...</a>
     *
     * @param qrcode 人员身份识别码。身份识别码打开方式：客户端（移动端）->我的->身份识别码
     */
    public String getStaffIdByQrcode(String qrcode) {
        String urlTpl = "%s/v1/qrcode/%s/staff/fetch?app_token=%s";
        String url = String.format(urlTpl, mLanxinAppConfig.getApiGateway(), qrcode, mLanxinTokenManager.getAppToken());
        JSONObject jsonObject = mLanxinLanxinHttpClient.get(url);
        return jsonObject.getString("staffId");
    }

    /**
     * 获取应用运营者
     * <a href="https://apigw-example.domain/v1/org/:orgid/role/:roldid/members/fetch?app_token=APP_TOKEN">...</a>
     *
     * @return 运营者列表，返回id和name
     */
    public JSONObject getOperators() {
        String OPERATORS_TEMPLATE = "%s/v1/org/%s/role/role_app_admin/members/fetch?app_token=%s";
        String url = String.format(OPERATORS_TEMPLATE, mLanxinAppConfig.getApiGateway(), mLanxinAppConfig.getOrgId(), mLanxinTokenManager.getAppToken());
        return mLanxinLanxinHttpClient.get(url);
    }




}
